﻿////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2008 Chris Colinsky
// All Rights Reserved
//
// version	0.4
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

import sugarcookie.events.EventController;
import sugarcookie.logger.Logger;

/**
 * Class is a wrapper for MovieClipLoader
 */
class sugarcookie.file.ClipLoader extends EventController {
	private var log:Logger;
	private var mcLoader:MovieClipLoader;
	
	/**
	 * Constructor - sets properties to default values and makes argument assignments
	 * 
	 */
	
	public function ClipLoader(){
		super(this);
		mcLoader = new MovieClipLoader();
		mcLoader.addListener(this);
		log = Logger.getLog();
	}
	
	/**
	 *Public Functions - loadClip and unloadClip
	 *
	 */
	
	public function loadClip(url:String, targ:MovieClip):Boolean{
		if(mcLoader.loadClip(url, targ)){
			return true;
		}else{
			return false;
		}
	}
	
	public function unloadClip(targ:MovieClip):Boolean{
		if(mcLoader.unloadClip(targ)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 *Private Functions - Event Listeners
	 *
	 */
	
	private function onLoadStart(target:MovieClip){
		postEvent("onClipLoaderStatus", "onLoadStart", null, false, {targ:target});
	}
	
	private function onLoadProgress(target:MovieClip, bytesLoaded:Number, bytesTotal:Number){
		postEvent("onClipLoaderStatus", "onLoadProgress", null, false, {targ:target, loaded:bytesLoaded, total:bytesTotal});
	}
	
	private function onLoadComplete(target:MovieClip){
		postEvent("onClipLoaderStatus", "onLoadComplete", null, false, {targ:target});
	}
	
	private function onLoadInit(target:MovieClip){
		postEvent("onClipLoaderStatus", "onLoadInit", null, false, {targ:target});
	}
	
	private function onLoadError(target:MovieClip, errorCode:String, httpStatus:Number){
		var str:String;
		switch (httpStatus){
			case 400: 
			str = "httpStatus: "+httpStatus+" Client Error - Bad Request";
			break;
			case 401: 
			str = "httpStatus: "+httpStatus+" Client Error - Unauthorized";
			break;
			case 402: 
			str = "httpStatus: "+httpStatus+" Client Error - Payment Required";
			break;
			case 403: 
			str = "httpStatus: "+httpStatus+" Client Error - Forbidden";
			break;
			case 404: 
			str = "httpStatus: "+httpStatus+" Client Error - Not Found";
			break;
			case 405: 
			str = "httpStatus: "+httpStatus+" Client Error - Method Not Allowed";
			break;
			case 406: 
			str = "httpStatus: "+httpStatus+" Client Error - Not Acceptable";
			break;
			case 407: 
			str = "httpStatus: "+httpStatus+" Client Error - Proxy Identification Required";
			break;
			case 408: 
			str = "httpStatus: "+httpStatus+" Client Error - Request Timeout";
			break;
			case 409: 
			str = "httpStatus: "+httpStatus+" Client Error - Conflict";
			break;
			case 410: 
			str = "httpStatus: "+httpStatus+" Client Error - Gone";
			break;
			case 411: 
			str = "httpStatus: "+httpStatus+" Client Error - Length Required";
			break;
			case 412: 
			str = "httpStatus: "+httpStatus+" Client Error - Precondition Failed";
			break;
			case 413: 
			str = "httpStatus: "+httpStatus+" Client Error - Request Entity Too Large";
			break;
			case 414: 
			str = "httpStatus: "+httpStatus+" Client Error - Request-URI Too Long";
			break;
			case 415: 
			str = "httpStatus: "+httpStatus+" Client Error - Unsupported Media Type";
			break;
			case 416: 
			str = "httpStatus: "+httpStatus+" Client Error - Requested Range Not Satisfiable";
			break;
			case 417: 
			str = "httpStatus: "+httpStatus+" Client Error - Expectation Failed";
			break;
			case 500: 
			str = "httpStatus: "+httpStatus+" Server Error - Internal Server Error";
			break;
			case 501: 
			str = "httpStatus: "+httpStatus+" Server Error - Not Implemented";
			break;
			case 502: 
			str = "httpStatus: "+httpStatus+" Server Error - Bad Gateway";
			break;
			case 503: 
			str = "httpStatus: "+httpStatus+" Server Error - Service Unavailable";
			break;
			case 504: 
			str = "httpStatus: "+httpStatus+" Server Error - Gateway Timeout";
			break;
			case 505: 
			str = "httpStatus: "+httpStatus+" Server Error - HTTP Version Not Supported";
			break;
			default:
			str = "UNKNOWN ERROR";
			break;
		}
		postEvent("onClipLoaderStatus", "onLoadError", null, true, {targ:target, httpStatus:str, errorCode:errorCode});
	}
	
}